/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.*;
import Module.*;


/**
 *
 * @author CCD gruppe 2
 */

public class ExperienceController {

    /**
     * Object used to connect to the database
     */
    private Database database = new Database();
    /**
     * Method used to register a excperience
     * @param experience
     * @return
     */
    public boolean registrerExperience(Experience experience){

        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            String[] autogen = {"id"};
            PreparedStatement statement = conn.prepareStatement(
                    "Insert into experience values(DEFAULT,?,?)",autogen);
            statement.setInt(1, experience.getProjectId());
            statement.setString(2,experience.getExperience());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch (Exception e) {
            System.err.println(e);

        }
        return false;
    }
    /**
     * Method used to edit a experience
     * @param experience
     * @return
     */
    public boolean editExperience(Experience experience){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("Update experience "
                    + "set projectId=?, experience=? where projectId =?");
            statement.setInt(1, experience.getProjectId());
            statement.setString(2, experience.getExperience());
            statement.setInt(3, experience.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }

        return false;
    }
    /**
     * Method used to delete an experience
     * @param experience
     * @return
     */
    public boolean deleteExperience(Experience experience){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from experience where projectId=?");
            statement.setInt(1,experience.getProjectId());
            statement.executeUpdate();
            conn.close();
            return false;
        }catch(Exception e){
            System.err.println(e);
        }


        return false;
    }
    /**
     * Method used to get the experience to a project
     * @param project
     * @return
     */
    public Experience getExperience(Project project){

        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from experience where projectId =?");
            statement.setInt(1, project.getId());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                return new Experience(rs.getInt(1),rs.getInt(2),rs.getString(3));
            }

        }catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
}

